package com.ihighest.app.infox.adapter;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.ihighest.app.infox.R;
import com.ihighest.app.infox.activity.OnlineVoteDetailActivity;

public class OnlineVoteDetailAdapter extends BaseAdapter {
	private Context context;
	private JSONArray array;
	private LayoutInflater inflater;
	private String join;// 表示已经参与的详情
	private int number;

	public OnlineVoteDetailAdapter(Context context) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
	}

	public OnlineVoteDetailAdapter(Context context, JSONArray array, int number) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.number = number;
		this.array = array;
	}

	public OnlineVoteDetailAdapter(Context context, JSONArray array, String join) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.array = array;
		this.join = join;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return array == null ? 0 : array.length();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup group) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.infox_activity_onlinevotedetail_answeritem, null);
			holder.bar = (ProgressBar) convertView.findViewById(R.id.chooseThisBar);
			holder.chooseReason = (CheckBox) convertView.findViewById(R.id.voteDetailItem);
			holder.voteNumber = (TextView) convertView.findViewById(R.id.chooseThisNumber);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.bar.setMax(number);

		try {
			int mcount = (array.getJSONObject(position).getInt("count") * 100) / number;
			holder.chooseReason.setText(array.getJSONObject(position).getString("title"));
			holder.voteNumber.setText("已投票" + mcount + "%");
			holder.bar.setProgress(mcount);
			if (!TextUtils.isEmpty(join)) {
				holder.chooseReason.setChecked(array.getJSONObject(position).getBoolean("flag"));

			}
			holder.chooseReason.setOnCheckedChangeListener(new OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(CompoundButton view, boolean isCheck) {
					try {
						String id = array.getJSONObject(position).getString("id");
						if (isCheck) {
							if (!OnlineVoteDetailActivity.chooseList.contains(id)) {
								// 如果list中不存在该条数据加入
								OnlineVoteDetailActivity.chooseList.add(id);
							}

						} else {
							if (OnlineVoteDetailActivity.chooseList.contains(id)) {
								// 如果list中存在该条数据删除
								OnlineVoteDetailActivity.chooseList.remove(id);
							}
						}
					} catch (JSONException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

				}
			});
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return convertView;
	}

	class ViewHolder {
		CheckBox chooseReason;
		TextView voteNumber;
		ProgressBar bar;

	}
}
